home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / lib2 / c_array.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  19.5 KB  |  848 lines

  1. /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2.  
  3. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  4. Permission is granted to reproduce this software for non-commercial
  5. purposes provided that this notice is left intact.
  6.  
  7. It is acknowledged that the U.S. Government has rights to this software
  8. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  9. and the University of California.
  10.  
  11. This software is provided as a professional and academic contribution
  12. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  13. with no warranties of any kind whatsoever, no support, no promise of
  14. updates, or printed documentation. By using this software, you
  15. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  16. University of California shall have no liability with respect to the
  17. infringement of other copyrights by any part of this software.
  18.  
  19. For further information about this notice, contact William Johnston,
  20. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  21. (wejohnston@lbl.gov)
  22.  
  23. For further information about this software, contact:
  24.     Jin Guojun
  25.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  26.     g_jin@lbl.gov
  27.  
  28. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  29.  *
  30.  *     c_array.c   a collection of routines for handeling
  31.  *                 dynamically allocated arrays in C.  
  32.  *
  33.  *     Brian Tierney  LBL
  34.  *
  35.  *  This routines allocate and access array by creating arrays of pointers.
  36.  *   The reason for these routines are speed and code readability. Allocating
  37.  *   arrays in this manner means that elements can be accessed by pointers
  38.  *   instead of by a multiplication and an addition. This method uses more 
  39.  *   memory, but on a machine with plenty of memory the increase in speed 
  40.  *   is worth it.  
  41.  *
  42.  *    for example, this:
  43.  * 
  44.  *       for (r = 0; r < nrow; r++)
  45.  *           for (c = 0; c < ncol; c++) {
  46.  *            pixel = image[r][c];
  47.  *
  48.  * is faster and more readable (in my opinion), than this:
  49.  *
  50.  *       for (r = 0; r < nrow; r++)
  51.  *           for (c = 0; c < ncol; c++) {
  52.  *            pixel = image[r * nrow + c];
  53.  */
  54.  
  55. /* routines in this file:
  56.  
  57.       alloc_3d_byte_array(nx,ny,nz)
  58.       alloc_2d_byte_array(nx,ny)
  59.       read_3d_byte_array(fp,array,nx,ny,nz)
  60.       read_2d_byte_array(fp,array,nx ,ny)
  61.       write_3d_byte_array(fp,array,nx,ny,nz)
  62.       write_2d_byte_array(fp,array,nx,ny)
  63.       free_3d_byte_array(array)
  64.       free_2d_byte_array(array)
  65.  
  66.   ** and same routines for short, int, and float
  67.  
  68.   *
  69.   *  all read/write routines return a 0 if successful and 1 otherwise 
  70.   *
  71.   *
  72.   *  sample use with hips images:  r=rows, c=cols, f = frames
  73.   *    2D case:
  74.   *    u_char **pic;
  75.   *    pic = alloc_2d_byte_array(r,c);
  76.   *    read_2d_byte_array(stdin, pic1, r,c);
  77.   *
  78.   *    for (i = 0; i < r; i++)
  79.   *    for (j = 0; j < c; j++)  {  * want column to vary fastest *
  80.   *        val = pic[i][j]; 
  81.   *    }
  82.   *    write_2d_byte_array(stdout, pic2, r,c); 
  83.   *
  84.   ****************************************************
  85.   *   3D case:
  86.   *    u_char **pic;
  87.   *    pic = alloc_3d_byte_array(f,r,c);
  88.   *  
  89.   *  read_3d_byte_array(stdin, pic, f,r,c);
  90.   *    
  91.   *   for (i = 0; i < f; i++)
  92.   *      for (j = 0; j < r; j++)  
  93.   *        for(k=0; k< c; k++) {  * vary col fastest, frame slowest *
  94.   *            val = pic[i][j][k]; 
  95.   */
  96.  
  97. /***********************************************************************/
  98. /*  COPYRIGHT NOTICE         *******************************************/
  99. /***********************************************************************/
  100.  
  101. /*   This program is copyright (C) 1990, Regents  of  the
  102. University  of  California.   Anyone may reproduce this software,
  103. in whole or in part, provided that:
  104. (1)  Any copy  or  redistribution  must  show  the
  105.      Regents  of  the  University of California, through its
  106.      Lawrence Berkeley Laboratory, as the source,  and  must
  107.      include this notice;
  108. (2)  Any use of this software must reference this  distribu-
  109.      tion,  state that the software copyright is held by the
  110.      Regents of the University of California, and  that  the
  111.      software is used by their permission.
  112.  
  113.      It is acknowledged that the U.S. Government has  rights
  114. to this software under  Contract DE-AC03-765F00098 between the U.S.
  115. Department of Energy and the University of California.
  116.  
  117.      This software is provided as a professional  academic
  118. contribution for  joint exchange.  Thus it is experimental, is
  119. provided ``as is'', with no warranties of any kind  whatsoever,
  120. no  support,  promise  of updates, or printed documentation.
  121. Bug reports or fixes may be sent to the author, who may or may
  122. not act on them as he desires.
  123. */
  124.  
  125. /*   Author:  Brian L. Tierney
  126.  *            Lawrence Berkeley Laboratory
  127.  *            Imaging and Distributed Computing Group
  128.  *            email: bltierney@lbl.gov
  129. */
  130.  
  131. #include <stdio.h>
  132. #include <sys/types.h>
  133.  
  134. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  135. #define Fread(a,b,c,d) fread((char *)(a), b, (int)(c), d)
  136. #define Fwrite(a,b,c,d) fwrite((char *)(a), b, (int)(c), d)
  137.  
  138. /*********************************/
  139. u_char ***
  140. alloc_3d_byte_array(nx,ny,nz)  /* in hips terminology: col,row,frame */
  141. int nx,ny,nz;
  142. {
  143.     u_char ***array;
  144.     register int i, j;
  145.  
  146.     /* allocate 3-d array for input image data */
  147.  
  148.     /* allocate 2 arrays of pointers */
  149.     if ((array = Calloc(nx, u_char **)) == NULL)
  150.     perror("calloc error: array ");
  151.     if ((array[0] = Calloc(nx * ny, u_char *)) == NULL)
  152.     perror("calloc error: array ");
  153.  
  154.     /* allocate array for data */
  155.     if ((array[0][0] = Calloc(nx * ny * nz, u_char)) ==    NULL)
  156.     perror("calloc error: array ");
  157.  
  158.     /* initialize pointer arrays */
  159.     for (i = 1; i < ny; i++)
  160.     array[0][i] = **array + (nz * i);
  161.     for (i = 1; i < nx; i++) {
  162.     array[i] = *array + (ny * i);
  163.     array[i][0] = **array + (nz * ny * i);
  164.     for (j = 1; j < ny; j++)/* initialize pointer array */
  165.         array[i][j] = array[i][0] + (nz * j);
  166.     }
  167.     return(array);
  168. }
  169.  
  170. /**********************************/
  171. u_char **
  172. alloc_2d_byte_array(nx,ny)
  173. int nx,ny;
  174. {
  175.     u_char **array;
  176.     register int i;
  177.  
  178.     /* allocate 2-d array for input image data */
  179.     /* allocate array of pointers */
  180.     if ((array = Calloc(nx, u_char *)) == NULL)
  181.     perror("calloc error: array ");
  182.  
  183.     /* allocate array for data */
  184.     if ((array[0] = Calloc(nx * ny, u_char)) == NULL)
  185.     perror("calloc error: array ");
  186.  
  187.     /* initialize pointer arrays */
  188.     for (i = 1; i < nx; i++) 
  189.     array[i] = array[0] + (ny * i);
  190.  
  191.     return(array);
  192. }
  193.  
  194. /**********************************/
  195. char **
  196. alloc_2d_char_array(nx,ny)
  197. int nx,ny;
  198. {
  199.     char **array;
  200.     register int i;
  201.  
  202.     /* allocate 2-d array for input image data */
  203.     /* allocate array of pointers */
  204.     if ((array = Calloc(nx, char *)) == NULL)
  205.     perror("calloc error: array ");
  206.  
  207.     /* allocate array for data */
  208.     if ((array[0] = Calloc(nx * ny, char)) == NULL)
  209.     perror("calloc error: array ");
  210.  
  211.     /* initialize pointer arrays */
  212.     for (i = 1; i < nx; i++) 
  213.     array[i] = array[0] + (ny * i);
  214.  
  215.     return(array);
  216. }
  217. /********************************/
  218. int
  219. read_3d_byte_array(fp,array,nx,ny,nz)
  220. FILE *fp;
  221. u_char ***array;
  222. int nx,ny,nz;
  223. {
  224.     long      rsize;
  225.  
  226.     rsize = nx * ny * nz; 
  227.     if (Fread(array[0][0], sizeof(u_char), rsize, fp)  != rsize) {
  228.     perror("\n error reading file\n");
  229.     return(-1);
  230.     }
  231.     return(0);
  232. }
  233.  
  234. /********************************/
  235. int
  236. read_2d_byte_array(fp,array,nx ,ny)
  237. FILE *fp;
  238. u_char **array;
  239. int nx,ny;
  240. {
  241.     long      rsize;
  242.  
  243.     rsize = nx * ny; 
  244.  
  245.     if (Fread(array[0], sizeof(u_char), rsize, fp)  != rsize) {
  246.         perror("\n error reading file\n"); 
  247.         return (-1);
  248.     } 
  249.  
  250.     return(0);
  251. }
  252.  
  253. /*******************************/
  254. int
  255. write_3d_byte_array(fp,array,nx,ny,nz)
  256. FILE *fp;
  257. u_char ***array;
  258. int nx,ny,nz;
  259. {
  260.     long      size;
  261.  
  262.     size = nx * ny * nz;  
  263.     if (Fwrite(array[0][0], sizeof(u_char), size, fp)  != size) {
  264.     perror("\n error writing file\n");
  265.     return (-1);
  266.     }
  267.  
  268.     return(0);
  269. }
  270.  
  271. /********************************/
  272. int
  273. write_2d_byte_array(fp,array,nx ,ny)
  274. FILE *fp;
  275. u_char **array;
  276. int nx,ny;
  277. {
  278.     long      size;
  279.  
  280.     size = nx* ny; 
  281.  
  282.     if (Fwrite(array[0], sizeof(u_char), size, fp)  != size) {
  283.     perror("\n error writing file\n");
  284.     return(-1);
  285.     }
  286.     return(0);
  287. }
  288.  
  289. /********************************/
  290. int
  291. free_3d_byte_array(array)
  292. u_char ***array;
  293. {
  294.     cfree((char *)array[0][0]);
  295.     cfree((char *)array[0]);
  296.     cfree((char *)array);
  297. }
  298.  
  299. /*********************************/
  300. int
  301. free_2d_byte_array(array)
  302. u_char **array;
  303. {
  304.     cfree((char *)array[0]);
  305.     cfree((char *)array);
  306. }
  307.  
  308. /********************************************************/
  309. /* same routines for data type short                    */
  310. /********************************************************/
  311.  
  312. short ***
  313. alloc_3d_short_array(nx,ny,nz)
  314. int nx,ny,nz;
  315. {
  316.     short ***array;
  317.     register int i, j;
  318.  
  319.     /* allocate 3-d array for input image data */
  320.  
  321.     /* allocate 2 arrays of pointers */
  322.     if ((array = Calloc(nx, short **)) == NULL)
  323.     perror("calloc error: array ");
  324.     if ((array[0] = Calloc(nx * ny, short *)) == NULL)
  325.     perror("calloc error: array ");
  326.  
  327.     /* allocate array for data */
  328.     if ((array[0][0] = Calloc(nx * ny * nz, short)) ==    NULL)
  329.     perror("calloc error: array ");
  330.  
  331.     /* initialize pointer arrays */
  332.     for (i = 1; i < ny; i++)
  333.     array[0][i] = **array + (nz * i);
  334.     for (i = 1; i < nx; i++) {
  335.     array[i] = *array + (ny * i);
  336.     array[i][0] = **array + (ny * nz * i);
  337.     for (j = 1; j < ny; j++)/* initialize pointer array */
  338.         array[i][j] = array[i][0] + (nz * j);
  339.     }
  340.     return(array);
  341. }
  342.  
  343. /**********************************/
  344. short **
  345. alloc_2d_short_array(nx,ny)
  346. int nx,ny;
  347. {
  348.     short **array;
  349.     register int i;
  350.  
  351.     /* allocate 2-d array for input image data */
  352.     /* allocate array of pointers */
  353.     if ((array = Calloc(nx, short *)) == NULL)
  354.     perror("calloc error: array ");
  355.  
  356.     /* allocate array for data */
  357.     if ((array[0] = Calloc(nx * ny, short)) == NULL)
  358.     perror("calloc error: array ");
  359.  
  360.     /* initialize pointer arrays */
  361.     for (i = 0; i < nx; i++) 
  362.     array[i] = *array + (ny * i);
  363.  
  364.     return(array);
  365. }
  366.  
  367. /********************************/
  368. int
  369. read_3d_short_array(fp,array,nx,ny,nz)
  370. FILE *fp;
  371. short ***array;
  372. int nx,ny,nz;
  373. {
  374.     long      rsize;
  375.  
  376.     rsize = nx * ny * nz; 
  377.     if (Fread(array[0][0], sizeof(short), rsize, fp)  != rsize) {
  378.     perror("\n error reading file\n");
  379.     return(-1);
  380.     }
  381.     return(0);
  382. }
  383.  
  384. /********************************/
  385. int
  386. read_2d_short_array(fp,array,nx ,ny)
  387. FILE *fp;
  388. short **array;
  389. int nx,ny;
  390. {
  391.     long      rsize;
  392.  
  393.     rsize = nx * ny; 
  394.     if (Fread(array[0], sizeof(short), rsize, fp)  != rsize) {
  395.         perror("\n error reading file\n");
  396.         return(-1);
  397.     }
  398.     return(0);
  399. }
  400.  
  401. /*******************************/
  402. int
  403. write_3d_short_array(fp,array,nx,ny,nz)
  404. FILE *fp;
  405. short ***array;
  406. int nx,ny,nz;
  407. {
  408.     long      size;
  409.  
  410.     size = nx * ny * nz;  
  411.     if (Fwrite(array[0][0], sizeof(short), size, fp)  != size) {
  412.     perror("\n error writing file\n");
  413.     return(-1);
  414.     }
  415.     return(0);
  416. }
  417.  
  418. /********************************/
  419. int
  420. write_2d_short_array(fp,array,nx ,ny)
  421. FILE *fp;
  422. short **array;
  423. int nx,ny;
  424. {
  425.     long      size;
  426.  
  427.     size = nx* ny; 
  428.  
  429.     if (Fwrite(array[0], sizeof(short), size, fp)  != size) {
  430.     perror("\n error writing file\n");
  431.     return(-1);
  432.     }
  433.     return(0);
  434. }
  435.  
  436. /********************************/
  437. int
  438. free_3d_short_array(array)
  439. short ***array;
  440. {
  441.     cfree((char *)array[0][0]);
  442.     cfree((char *)array[0]);
  443.     cfree((char *)array);
  444. }
  445.  
  446. /*********************************/
  447. int
  448. free_2d_short_array(array)
  449. short **array;
  450. {
  451.     cfree((char *)array[0]);
  452.     cfree((char *)array);
  453. }
  454.  
  455. /****************************************************/
  456. /*  int routines */
  457. /**************************************************/
  458.  
  459. int ***
  460. alloc_3d_int_array(nx,ny,nz)
  461. int nx,ny,nz;
  462. {
  463.     int ***array;
  464.     register int i, j;
  465.  
  466.     /* allocate 3-d array for input image data */
  467.  
  468.     /* allocate 2 arrays of pointers */
  469.     if ((array = Calloc(nx, int **)) == NULL)
  470.     perror("calloc error: array ");
  471.     if ((array[0] = Calloc(nx * ny, int *)) == NULL)
  472.     perror("calloc error: array ");
  473.  
  474.     /* allocate array for data */
  475.     if ((array[0][0] = Calloc(nx * ny * nz, int)) ==    NULL)
  476.     perror("calloc error: array ");
  477.  
  478.     /* initialize pointer arrays */
  479.     for (i = 1; i < ny; i++)
  480.     array[0][i] = **array + (nz * i);
  481.     for (i = 1; i < nx; i++) {
  482.     array[i] = *array + (ny * i);
  483.     array[i][0] = **array + (ny * nz * i);
  484.     for (j = 1; j < ny; j++)/* initialize pointer array */
  485.         array[i][j] = array[i][0] + (nz * j);
  486.     }
  487.     return(array);
  488. }
  489.  
  490. /**********************************/
  491. int **
  492. alloc_2d_int_array(nx,ny)
  493. int nx,ny;
  494. {
  495.     int **array;
  496.     register int i;
  497.  
  498.     /* allocate 2-d array for input image data */
  499.     /* allocate array of pointers */
  500.     if ((array = Calloc(nx, int *)) == NULL)
  501.     perror("calloc error: array ");
  502.  
  503.     /* allocate array for data */
  504.     if ((array[0] = Calloc(nx * ny, int)) == NULL)
  505.     perror("calloc error: array ");
  506.  
  507.     /* initialize pointer arrays */
  508.     for (i = 0; i < nx; i++) 
  509.     array[i] = *array + (ny * i);
  510.  
  511.     return(array);
  512. }
  513.  
  514. /********************************/
  515. int
  516. read_3d_int_array(fp,array,nx,ny,nz)
  517. FILE *fp;
  518. int ***array;
  519. int nx,ny,nz;
  520. {
  521.     long      rsize;
  522.  
  523.     rsize = nx * ny * nz; 
  524.     if (Fread(array[0][0], sizeof(int), rsize, fp)  != rsize) {
  525.     perror("\n error reading file\n");
  526.     return(-1);
  527.     }
  528.     return(0);
  529. }
  530.  
  531. /********************************/
  532. int
  533. read_2d_int_array(fp,array,nx ,ny)
  534. FILE *fp;
  535. int **array;
  536. int nx,ny;
  537. {
  538.     long      rsize;
  539.  
  540.     rsize = nx * ny; 
  541.     if (Fread(array[0], sizeof(int), rsize, fp)  != rsize) {
  542.         perror("\n error reading file\n");
  543.         return(-1);
  544.     }
  545.     return(0);
  546. }
  547.  
  548. /*******************************/
  549. int
  550. write_3d_int_array(fp,array,nx,ny,nz)
  551. FILE *fp;
  552. int ***array;
  553. int nx,ny,nz;
  554. {
  555.     long      size;
  556.  
  557.     size = nx * ny * nz;  
  558.     if (Fwrite(array[0][0], sizeof(int), size, fp)  != size) {
  559.     perror("\n error writing file\n");
  560.     return(-1);
  561.     }
  562.     return(0);
  563. }
  564.  
  565. /********************************/
  566. int
  567. write_2d_int_array(fp,array,nx ,ny)
  568. FILE *fp;
  569. int **array;
  570. int nx,ny;
  571. {
  572.     long      size;
  573.  
  574.     size = nx* ny; 
  575.  
  576.     if (Fwrite(array[0], sizeof(int), size, fp)  != size) {
  577.     perror("\n error writing file\n");
  578.     return(-1);
  579.     }
  580.     return(0);
  581. }
  582.  
  583. /********************************/
  584. int
  585. free_3d_int_array(array)
  586. int ***array;
  587. {
  588.     cfree((char *)array[0][0]);
  589.     cfree((char *)array[0]);
  590.     cfree((char *)array);
  591. }
  592.  
  593. /*********************************/
  594. int
  595. free_2d_int_array(array)
  596. int **array;
  597. {
  598.     cfree((char *)array[0]);
  599.     cfree((char *)array);
  600. }
  601.  
  602. /****************************************************/
  603. /*  float routines */
  604. /**************************************************/
  605.  
  606. float ***
  607. alloc_3d_float_array(nx,ny,nz)
  608. int nx,ny,nz;
  609. {
  610.     float ***array;
  611.     register int i, j;
  612.  
  613.     /* allocate 3-d array for input image data */
  614.  
  615.     /* allocate 2 arrays of pointers */
  616.     if ((array = Calloc(nx, float **)) == NULL)
  617.     perror("calloc error: array ");
  618.     if ((array[0] = Calloc(nx * ny, float *)) == NULL)
  619.     perror("calloc error: array ");
  620.  
  621.     /* allocate array for data */
  622.     if ((array[0][0] = Calloc(nx * ny * nz, float)) ==    NULL)
  623.     perror("calloc error: array ");
  624.  
  625.     /* initialize pointer arrays */
  626.     for (i = 1; i < ny; i++)
  627.     array[0][i] = **array + (nz * i);
  628.     for (i = 1; i < nx; i++) {
  629.     array[i] = *array + (ny * i);
  630.     array[i][0] = **array + (ny * nz * i);
  631.     for (j = 1; j < ny; j++)/* initialize pointer array */
  632.         array[i][j] = array[i][0] + (nz * j);
  633.     }
  634.     return(array);
  635. }
  636.  
  637. /**********************************/
  638. float **
  639. alloc_2d_float_array(nx,ny)
  640. int nx,ny;
  641. {
  642.     float **array;
  643.     register int i;
  644.  
  645.     /* allocate 2-d array for input image data */
  646.     /* allocate array of pointers */
  647.     if ((array = Calloc(nx, float *)) == NULL)
  648.     perror("calloc error: array ");
  649.  
  650.     /* allocate array for data */
  651.     if ((array[0] = Calloc(nx * ny, float)) == NULL)
  652.     perror("calloc error: array ");
  653.  
  654.     /* initialize pointer arrays */
  655.     for (i = 0; i < nx; i++) 
  656.     array[i] = *array + (ny * i);
  657.  
  658.     return(array);
  659. }
  660.  
  661. /********************************/
  662. int
  663. read_3d_float_array(fp,array,nx,ny,nz)
  664. FILE *fp;
  665. float ***array;
  666. int nx,ny,nz;
  667. {
  668.     long      rsize;
  669.  
  670.     rsize = nx * ny * nz; 
  671.     if (Fread(array[0][0], sizeof(float), rsize, fp)  != rsize) {
  672.     perror("\n error reading file\n");
  673.     return(-1);
  674.     }
  675.     return(0);
  676. }
  677.  
  678. /********************************/
  679. int
  680. read_2d_float_array(fp,array,nx ,ny)
  681. FILE *fp;
  682. float **array;
  683. int nx,ny;
  684. {
  685.     long      rsize;
  686.  
  687.     rsize = nx * ny; 
  688.     if (Fread(array[0], sizeof(float), rsize, fp)  != rsize) {
  689.         perror("\n error reading file\n");
  690.         return(-1);
  691.     }
  692.     return(0);
  693. }
  694.  
  695. /*******************************/
  696. int
  697. write_3d_float_array(fp,array,nx,ny,nz)
  698. FILE *fp;
  699. float ***array;
  700. int nx,ny,nz;
  701. {
  702.     long      size;
  703.  
  704.     size = nx * ny * nz;  
  705.     if (Fwrite(array[0][0], sizeof(float), size, fp)  != size) {
  706.     perror("\n error writing file\n");
  707.     return(-1);
  708.     }
  709.     return(0);
  710. }
  711.  
  712. /********************************/
  713. int
  714. write_2d_float_array(fp,array,nx ,ny)
  715. FILE *fp;
  716. float **array;
  717. int nx,ny;
  718. {
  719.     long      size;
  720.  
  721.     size = nx* ny; 
  722.  
  723.     if (Fwrite(array[0], sizeof(float), size, fp)  != size) {
  724.     perror("\n error writing file\n");
  725.     return(-1);
  726.     }
  727.     return(0);
  728. }
  729.  
  730. /********************************/
  731. int
  732. free_3d_float_array(array)
  733. float ***array;
  734. {
  735.     cfree((char *)array[0][0]);
  736.     cfree((char *)array[0]);
  737.     cfree((char *)array);
  738. }
  739.  
  740. /*********************************/
  741. int
  742. free_2d_float_array(array)
  743. float **array;
  744. {
  745.     cfree((char *)array[0]);
  746.     cfree((char *)array);
  747. }
  748.  
  749. /**************************************************************/
  750. /*                  long routines                             */
  751. /**************************************************************/
  752.  
  753. long ***
  754. alloc_3d_long_array(nx,ny,nz)  /* in hips terminology: col,row,frame */
  755. int nx,ny,nz;
  756. {
  757.     long ***array;
  758.     register int i, j;
  759.  
  760.     /* allocate 3-d array for input image data */
  761.  
  762.     /* allocate 2 arrays of pointers */
  763.     if ((array = Calloc(nx, long **)) == NULL)
  764.     perror("calloc error: array ");
  765.     if ((array[0] = Calloc(nx * ny, long *)) == NULL)
  766.     perror("calloc error: array ");
  767.  
  768.     /* allocate array for data */
  769.     if ((array[0][0] = Calloc(nx * ny * nz, long)) ==    NULL)
  770.     perror("calloc error: array ");
  771.  
  772.     /* initialize pointer arrays */
  773.     for (i = 1; i < ny; i++)
  774.     array[0][i] = **array + (nz * i);
  775.     for (i = 1; i < nx; i++) {
  776.     array[i] = *array + (ny * i);
  777.     array[i][0] = **array + (nz * ny * i);
  778.     for (j = 1; j < ny; j++)/* initialize pointer array */
  779.         array[i][j] = array[i][0] + (nz * j);
  780.     }
  781.     return(array);
  782. }
  783.  
  784. /**********************************/
  785. long **
  786. alloc_2d_long_array(nx,ny)
  787. int nx,ny;
  788. {
  789.     long **array;
  790.     register int i;
  791.  
  792.     /* allocate 2-d array for input image data */
  793.     /* allocate array of pointers */
  794.     if ((array = Calloc(nx, long *)) == NULL)
  795.     perror("calloc error: array ");
  796.  
  797.     /* allocate array for data */
  798.     if ((array[0] = Calloc(nx * ny, long)) == NULL)
  799.     perror("calloc error: array ");
  800.  
  801.     /* initialize pointer arrays */
  802.     for (i = 0; i < nx; i++) 
  803.     array[i] = *array + (ny * i);
  804.  
  805.     return(array);
  806. }
  807.  
  808. /********************************/
  809. int
  810. free_3d_long_array(array)
  811. long ***array;
  812. {
  813.     cfree((char *)array[0][0]);
  814.     cfree((char *)array[0]);
  815.     cfree((char *)array);
  816. }
  817.  
  818. /*********************************/
  819. int
  820. free_2d_long_array(array)
  821. long **array;
  822. {
  823.     cfree((char *)array[0]);
  824.     cfree((char *)array);
  825. }
  826.  
  827. /***********************************************************************/
  828. /*  all types: all use these if array is cast when calling the routine */
  829. /***********************************************************************/
  830. int
  831. free_3d_array(array)
  832. char ***array;
  833. {
  834.     cfree(array[0][0]);
  835.     cfree(array[0]);
  836.     cfree(array);
  837. }
  838.  
  839. /*********************************/
  840. int
  841. free_2d_array(array)
  842. char **array;
  843. {
  844.     cfree(array[0]);
  845.     cfree(array);
  846. }
  847.  
  848.